package 创建的方式;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class 方式三_Callable2 {
    static int i = 0;

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        Callable<Integer> r = new Callable<Integer>() {
            @Override
            public Integer call() {
                try {
                    //模拟执行一段任务
                    Thread.sleep(1000);
                    return 1;
                } catch (InterruptedException e) {
                    throw new RuntimeException("出错了");
                }
            }
        };
        FutureTask<Integer> task = new FutureTask<>(r);
        Thread t = new Thread(task);
        t.start();
        //想看看t执行的结果: get会让当前线程等待，直到t线程执行完，并获取到Callable的返回值
        System.out.println(task.get());
    }
}
